var app = getApp();
Page({
  data: {
    list:[],
    cart_count:'0',
    subtotal:'0'
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onShow:function(){
    this.getcarts();
  },
  onReady: function () {
    // 页面渲染完成
  },

  goList:function(e){
    wx.reLaunch({
      url:"../index/index"
    })
  },

  getcarts() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    let token = wx.getStorageSync('token');
    if (token) {
      app.getUserToken(function (err, token) {
        wx.request({
          url: app.globalData.url +'/index.php?_m=checkout/cart/get_cart',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          method: 'POST',
          data: { auth_token: token },
          success: (res) => {
            wx.hideLoading()
            if (res.data.status == 401) {
              // 清空授权token
              app.globalData.token = null
              wx.removeStorageSync('token')
              that.getcarts();
            } else if (res.data.code == 0) {
              that.setData({
                list: res.data.data.products,
                cart_count: res.data.data.cart_count,
                subtotal: res.data.data.subtotal
              })
            }
          },
          fail: (res) => {
            wx.showToast({
              title: '获取借口失败',
              duration: 2000
            })
          }
        })
      })
    } else {
      wx.hideLoading()
      var cartItems = wx.getStorageSync('cartItems') || []
      let cart_count = 0;
      let subtotal = 0;
      if (cartItems) {
        for (var i = 0; i < cartItems.length; i++) {
          cart_count += parseInt(cartItems[i].qty)
          subtotal += cartItems[i].qty * cartItems[i].price
        }
      }
      that.setData({
        list: cartItems,
        cart_count: cart_count,
        subtotal: subtotal.toFixed(2)
      })

    }
  },
  
  bluriuput: function (e) {
    let that=this;
    let qty=e.detail.value;
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.id;
      wx.showLoading({
        title: '加载中...',
      })
    let token = wx.getStorageSync('token');
    if (token) {
      app.getUserToken(function (err, token) {
        wx.request({
          url: app.globalData.url +'/index.php?_m=checkout/cart/update_cart&v=v20190909b',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: { prid: id, qty: qty},
          method: 'POST',
          success: (res) => {
            wx.hideLoading();
            if (res.data.status == 401) {
              // 清空授权token
              app.globalData.token = null
              wx.removeStorageSync('token')
              that.getcarts(prid, qty);
            } else if (res.data.code == 0) {
              that.getcarts();
            }
          }
        })
      })
    }else{
      wx.hideLoading();
      var cartItems = wx.getStorageSync('cartItems') || []
      cartItems[index].qty = qty
      wx.setStorageSync("cartItems", cartItems)
      that.getcarts(); 
    }
  },

delProduct: function(e){
    var that = this;
    let id = e.currentTarget.id;
    let item_id = e.currentTarget.dataset.index;

    var that = this;

    wx.showModal({
      title: '提示',
      content: '是否要删除选择中商品？',
      success: function (res) {
        wx.showLoading({
          title: '加载中...',
        })
        if (res.confirm){
          let token = wx.getStorageSync('token');
          if (token) {
            app.getUserToken(function (err, token) {
            wx.request({
              url: app.globalData.url +'/index.php?_m=checkout/cart/update_cart&v=v20190909b',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: { prid: id, qty: 0,auth_token: token },
              method: 'POST',
              success: (res) => {
                wx.hideLoading();
                  if (res.data.status == 401) {
                  // 清空授权token
                  app.globalData.token = null
                    wx.removeStorageSync('token')
                    that.getcarts(prid, qty);
                  } else if (res.data.code == 0) {
                    that.getcarts();
                  }
                }
              })
            })
          }else{
            wx.hideLoading();
            var cartItems = wx.getStorageSync('cartItems') || []
            cartItems.splice(item_id, 1)
            wx.setStorageSync("cartItems", cartItems)
            that.getcarts();
          }
        }else{
          wx.hideLoading();
        }
      }
    })
},

  lessclick: function (e) {
    var that = this;
    let qty = e.currentTarget.dataset.qty;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    if(qty == 1){
      wx.showToast({
        title: '无库存',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    let num = parseFloat(qty) - 1;
      wx.showLoading({
        title: '加载中...',
      })
 
    let token = wx.getStorageSync('token');
    if (token) {
      app.getUserToken(function (err, token) {
        wx.request({
          url: app.globalData.url +'/index.php?_m=checkout/cart/update_cart&v=v20190909b',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: { prid: id, qty: num, auth_token: token },
          method: 'POST',
          success: (res) => {
            wx.hideLoading();
            if (res.data.status == 401) {
              // 清空授权token
              app.globalData.token = null
              wx.removeStorageSync('token')
              that.getcarts(prid, qty);
            } else if (res.data.code == 0) {
              that.getcarts();
            }
          }
        })
      })
    }else{
      wx.hideLoading();
      var cartItems = wx.getStorageSync('cartItems') || []
      cartItems[index].qty = num
      wx.setStorageSync("cartItems", cartItems)
      that.getcarts(); 
    }
  },

  addclick: function (e) {
    var that = this;
    let qty = e.currentTarget.dataset.qty;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let num = parseFloat(qty) + 1;
      wx.showLoading({
        title: '加载中...',
      })

    let token = wx.getStorageSync('token');
    if (token) {
    app.getUserToken(function (err, token) {
        wx.request({
          url: app.globalData.url +'/index.php?_m=checkout/cart/update_cart&v=v20190909b',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: { prid: id, qty: num, auth_token: token },
          method: 'POST',
          success: (res) => {
            wx.hideLoading();
            if (res.data.status == 401) {
              // 清空授权token
              app.globalData.token = null
              wx.removeStorageSync('token')
              that.getcarts(prid, qty);
            } else if (res.data.code == 0) {
              that.getcarts();
            }
          }
        })
      })
    }else{
        wx.hideLoading();
        var cartItems = wx.getStorageSync('cartItems') || []
        cartItems[index].qty = num
        wx.setStorageSync("cartItems", cartItems)
        that.getcarts();     
    }
  },

  loginAndCart: function (e){
    app.getUserToken(function (err, token) {
      wx.navigateTo({
        url: '../payAddress/payAddress'
      })
    })
  },
  
  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }


})